import { Stripe } from 'stripe'

const stripe = new Stripe('sk_test_51R2tSZP8ixH7RW52fKzPIDor5dqaoW2OBdJUmBMhtw06FUml8wOLyZ9HUq01TLJrGKx5Yl8w7bEFGc9pwUAw2Yqa00yL0KGCHW', {
    apiVersion: '2025-02-24.acacia; custom_checkout_beta=v1' as any,
})

const YOUR_DOMAIN = 'http://localhost:4242'
export default defineEventHandler(async (event) => {
    const query = getQuery(event) as any

    const params: Stripe.Checkout.SessionCreateParams = {
        // payment_method_types: ['link', 'card'],
        line_items: [
            {
                price: 'price_1R2toUP8ixH7RW52fb8IXjqH',
                quantity: 1,
            },
        ],
        mode: 'payment',
        customer_email: 'john.doe@example.com',
    }

    if (query.mode === 'embedded') {
        params.ui_mode = 'embedded'
        params.redirect_on_completion = 'if_required'
        params.return_url = `${YOUR_DOMAIN}/return.html?session_id={CHECKOUT_SESSION_ID}`
    } else if (query.mode === 'custom') {
        params.ui_mode = 'custom' as any
        // params.saved_payment_method_options = {
        //     payment_method_save: 'enabled',
        //     // allow_redisplay_filters: ['always', 'limited'],
        // };
        params.return_url = `${YOUR_DOMAIN}/return.html?session_id={CHECKOUT_SESSION_ID}`
    }

    const session = await stripe.checkout.sessions.create(params)

    return {
        data: {
            clientSecret: session.client_secret,
        },
        code: 0,
        message: 'success',
    }
})
